package com.wmh.baseservice.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wmh.baseservice.common.utils.CommUtil;
import com.wmh.baseservice.common.utils.page.pojo.PageInfo;
import com.wmh.baseservice.common.utils.page.pojo.PageResult;
import com.wmh.baseservice.common.utils.page.pojo.SortWay;
import com.wmh.baseservice.common.utils.page.util.PageUtil;
import com.wmh.baseservice.common.utils.returnbody.ResultCodeInfo;
import com.wmh.baseservice.common.utils.returnbody.ReturnBody;
import com.wmh.baseservice.system.pojo.SysCategory;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import com.wmh.baseservice.system.service.SysCategoryService;

import java.util.List;

/**
 * @author wmh generator
 * @since 2021-12-26
 */
@Api(tags = "系统分类表")
@RestController
@RequestMapping(value = "system/sysCategory", produces = "text/plain;charset=utf-8")
@RequiresRoles(logical = Logical.OR, value = {"admin", "merchant"})
public class SysCategoryController {

    @Resource
    private SysCategoryService sysCategoryService;


    @ApiOperation("列表")
    @PostMapping("list")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataTypeClass = Long.class, name = "id", value = "id", required = false),
    })
    public String list(PageInfo pageInfo, Long id, Integer type) {
        pageInfo.setTimeScreen("create_time");
        pageInfo.setDefaultSort("create_time", SortWay.DESC);
        //执行分页辅助工具
        PageResult<SysCategory> pageResult = new PageUtil<SysCategory>(pageInfo).startPage((page, queryWrapper) -> {
            if (CommUtil.checkNull(id)) {
                queryWrapper.eq(SysCategory.ID, id);
            }
            if (CommUtil.checkNull(type)) {
                queryWrapper.eq(SysCategory.TYPE, type);
            }
            sysCategoryService.page(page, queryWrapper);
        });
        return ReturnBody.success(pageResult);
    }

    @ApiOperation("列表")
    @PostMapping("listNoPage")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataTypeClass = Long.class, name = "id", value = "id", required = false),
    })
    public String listNoPage(Integer type) {
        QueryWrapper<SysCategory> sysCategoryQueryWrapper = new QueryWrapper<>();
        if (CommUtil.checkNull(type)) {
            sysCategoryQueryWrapper.eq(SysCategory.TYPE, type);
        }
        List<SysCategory> sysCategories = sysCategoryService.listForUpdate(sysCategoryQueryWrapper);
        return ReturnBody.success(sysCategories);
    }

    @ApiOperation("删除")
    @PostMapping("delete")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataTypeClass = Long.class, name = "id", value = "id", required = false),
    })
    public String delete(Long id) {
        SysCategory sysCategory = sysCategoryService.getByIdForUpdate(id);
        if (!CommUtil.checkNull(sysCategory)){
            return ReturnBody.error(ResultCodeInfo.PARAM_ERROR);
        }
        //删除分类的时候要判断是否有使用该分类
        if (!sysCategoryService.removeById(sysCategory)) {
            return ReturnBody.error(ResultCodeInfo.SERVICE_EXCEPTION);
        }
        return ReturnBody.success();
    }

    @ApiOperation("添加或修改")
    @PostMapping("addOrUpdate")
    @ApiImplicitParams({
             @ApiImplicitParam(paramType = "query", dataTypeClass = Long.class, name = "id", value = "id", required = false),
     })
    public String addOrUpdate(SysCategory sysCategory){
        if (!sysCategoryService.saveOrUpdate(sysCategory)) {
            return ReturnBody.error(ResultCodeInfo.SERVICE_EXCEPTION);
        }
        return ReturnBody.success();
    }

}
